package com.yicao.yicaomall.product.service.impl;

import com.yicao.yicaomall.product.dao.BrandDao;
import com.yicao.yicaomall.product.dao.CategoryDao;
import com.yicao.yicaomall.product.entity.BrandEntity;
import com.yicao.yicaomall.product.service.BrandService;
import com.yicao.yicaomall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yicao.yicaomall.common.utils.PageUtils;
import com.yicao.yicaomall.common.utils.Query;

import com.yicao.yicaomall.product.dao.CategoryBrandRelationDao;
import com.yicao.yicaomall.product.entity.CategoryBrandRelationEntity;
import com.yicao.yicaomall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private BrandDao brandDao;
    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;
    @Autowired
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * 保存品牌与目录关系的详细信息
     *
     * @param categoryBrandRelation 品牌与目录关系实体对象
     */
    @Override
    public void saveDetails(CategoryBrandRelationEntity categoryBrandRelation) {
        // 获取品牌名称
        String brandName = brandDao.selectById(categoryBrandRelation.getBrandId()).getName();
        // 获取目录名称
        String catelogName = categoryDao.selectById(categoryBrandRelation.getCatelogId()).getName();
        // 设置品牌名称
        categoryBrandRelation.setBrandName(brandName);
        // 设置目录名称
        categoryBrandRelation.setCatelogName(catelogName);
        // 保存品牌与目录关系实体对象
        this.save(categoryBrandRelation);
    }

    /**
     * 获取某个分类下所有品牌信息
     */
    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> catelogId = categoryBrandRelationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        // 根据品牌id查询详细信息
        //List<Long> brandIds = catelogId.stream().map(item->{
        //            return item.getBrandId();
        //        }).collect(Collectors.toList());
        List<Long> brandIds = catelogId.stream().map(CategoryBrandRelationEntity::getBrandId).collect(Collectors.toList());
        return brandService.listByIds(brandIds);
    }


}
